﻿unit PmItemDataModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TPmItemDataModel = class(TObject)
  public 
    const cModuleKey='PmItemData'; 
    const cTblName ='pm_item_data';
    const cBaseView = 'view_pm_item_data';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cItemNoKey='item_no';
    const cItemNameKey='item_name';
    const cItemSpecKey='item_spec';
    const cItemStatusKey='item_status';
    const cItemSortNoKey='item_sort_no';
    const cItemTypeKey='item_type';
    const cItemSourceKey='item_source';
    const cItemRemarkKey='item_remark';
    const cItemClassIdKey='item_class_id';
    const cMsunitIdKey='msunit_id';
    const cPackageTypeIdKey='package_type_id';
    const cRanksIdKey='ranks_id';
    const cRecordPsnKey='record_psn';
    const cRecordDateKey='record_date';
    const cModifyPsnKey='modify_psn';
    const cModifyDateKey='modify_date';
    const cTimeStampKey='time_stamp';
  end;
  
  TPmItemData = class(TObject)
  private    
    fItemNo:string;
    fItemName:string;
    fItemSpec:string;
    fItemStatus:Integer;
    fItemSortNo:Integer;
    fItemType:Integer;
    fItemSource:Integer;
    fItemRemark:string;
    fItemClassId:string;
    fMsunitId:string;
    fPackageTypeId:string;
    fRanksId:string;
    fRecordPsn:string;
    fRecordDate:TDateTime;
    fModifyPsn:string;
    fModifyDate:TDateTime;
    fTimeStamp:Int64;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property ItemNo:string read fItemNo write fItemNo;
    property ItemName:string read fItemName write fItemName;
    property ItemSpec:string read fItemSpec write fItemSpec;
    property ItemStatus:Integer read fItemStatus write fItemStatus;
    property ItemSortNo:Integer read fItemSortNo write fItemSortNo;
    property ItemType:Integer read fItemType write fItemType;
    property ItemSource:Integer read fItemSource write fItemSource;
    property ItemRemark:string read fItemRemark write fItemRemark;
    property ItemClassId:string read fItemClassId write fItemClassId;
    property MsunitId:string read fMsunitId write fMsunitId;
    property PackageTypeId:string read fPackageTypeId write fPackageTypeId;
    property RanksId:string read fRanksId write fRanksId;
    property RecordPsn:string read fRecordPsn write fRecordPsn;
    property RecordDate:TDateTime read fRecordDate write fRecordDate;
    property ModifyPsn:string read fModifyPsn write fModifyPsn;
    property ModifyDate:TDateTime read fModifyDate write fModifyDate;
    property TimeStamp:Int64 read fTimeStamp write fTimeStamp;
  end;

  TPmItemDataList = class(TDictionary<string,TPmItemData>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TPmItemData;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TPmItemData):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TPmItemData;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TPmItemDataModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TPmItemDataModel.cTblName;
    dblmTable: ReadName:=TPmItemDataModel.cTblName;
    dblmView: ReadName:=TPmItemDataModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TPmItemDataModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TPmItemDataModel.cTblName;
    dblmTable: ReadName:=TPmItemDataModel.cTblName;
    dblmView: ReadName:=TPmItemDataModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TPmItemDataModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TPmItemDataModel.cTblName;
    dblmTable: ReadName:=TPmItemDataModel.cTblName;
    dblmView: ReadName:=TPmItemDataModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TPmItemData.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TPmItemData.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TPmItemData.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fItemNo:=EmptyStr;
  fItemName:=EmptyStr;
  fItemSpec:=EmptyStr;
  fItemStatus:=-1;
  fItemSortNo:=-1;
  fItemType:=-1;
  fItemSource:=-1;
  fItemRemark:=EmptyStr;
  fItemClassId:=EmptyStr;
  fMsunitId:=EmptyStr;
  fPackageTypeId:=EmptyStr;
  fRanksId:=EmptyStr;
  fRecordPsn:=EmptyStr;
  fRecordDate:=fInitDate;
  fModifyPsn:=EmptyStr;
  fModifyDate:=fInitDate;
  fTimeStamp:=-1;
end;

function TPmItemData.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TPmItemDataModel.cItemNoKey)<>nil) then
    begin
      fItemNo:=iCds.FieldByName(TPmItemDataModel.cItemNoKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fItemNo);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemNameKey)<>nil) then
    begin
      fItemName:=iCds.FieldByName(TPmItemDataModel.cItemNameKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemSpecKey)<>nil) then
    begin
      fItemSpec:=iCds.FieldByName(TPmItemDataModel.cItemSpecKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemSpecKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemStatusKey)<>nil) then
    begin
      fItemStatus:=iCds.FieldByName(TPmItemDataModel.cItemStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemSortNoKey)<>nil) then
    begin
      fItemSortNo:=iCds.FieldByName(TPmItemDataModel.cItemSortNoKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemTypeKey)<>nil) then
    begin
      fItemType:=iCds.FieldByName(TPmItemDataModel.cItemTypeKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemSourceKey)<>nil) then
    begin
      fItemSource:=iCds.FieldByName(TPmItemDataModel.cItemSourceKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemSourceKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemRemarkKey)<>nil) then
    begin
      fItemRemark:=iCds.FieldByName(TPmItemDataModel.cItemRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemClassIdKey)<>nil) then
    begin
      fItemClassId:=iCds.FieldByName(TPmItemDataModel.cItemClassIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemClassIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cMsunitIdKey)<>nil) then
    begin
      fMsunitId:=iCds.FieldByName(TPmItemDataModel.cMsunitIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cMsunitIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cPackageTypeIdKey)<>nil) then
    begin
      fPackageTypeId:=iCds.FieldByName(TPmItemDataModel.cPackageTypeIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cPackageTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cRanksIdKey)<>nil) then
    begin
      fRanksId:=iCds.FieldByName(TPmItemDataModel.cRanksIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cRanksIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cRecordPsnKey)<>nil) then
    begin
      fRecordPsn:=iCds.FieldByName(TPmItemDataModel.cRecordPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cRecordDateKey)<>nil) then
    begin
      if iCds.FindField(TPmItemDataModel.cRecordDateKey).IsNull then
      begin
        fRecordDate:=fInitDate;
      end else begin
        fRecordDate:=iCds.FieldByName(TPmItemDataModel.cRecordDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cModifyPsnKey)<>nil) then
    begin
      fModifyPsn:=iCds.FieldByName(TPmItemDataModel.cModifyPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cModifyDateKey)<>nil) then
    begin
      if iCds.FindField(TPmItemDataModel.cModifyDateKey).IsNull then
      begin
        fModifyDate:=fInitDate;
      end else begin
        fModifyDate:=iCds.FieldByName(TPmItemDataModel.cModifyDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cTimeStampKey)<>nil) then
    begin
      fTimeStamp:=iCds.FieldByName(TPmItemDataModel.cTimeStampKey).AsLargeInt;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemData.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TPmItemDataModel.cItemNoKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemNoKey).AsString:=fItemNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemNameKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemNameKey).AsString:=fItemName;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemNameKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemSpecKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemSpecKey).AsString:=fItemSpec;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemSpecKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemStatusKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemStatusKey).AsInteger:=fItemStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemSortNoKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemSortNoKey).AsInteger:=fItemSortNo;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemSortNoKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemTypeKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemTypeKey).AsInteger:=fItemType;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemSourceKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemSourceKey).AsInteger:=fItemSource;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemSourceKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemRemarkKey).AsString:=fItemRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cItemClassIdKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cItemClassIdKey).AsString:=fItemClassId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cItemClassIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cMsunitIdKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cMsunitIdKey).AsString:=fMsunitId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cMsunitIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cPackageTypeIdKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cPackageTypeIdKey).AsString:=fPackageTypeId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cPackageTypeIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cRanksIdKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cRanksIdKey).AsString:=fRanksId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cRanksIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cRecordPsnKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cRecordPsnKey).AsString:=fRecordPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cRecordDateKey)<>nil) then
    begin
      if SameDateTime(fRecordDate,fInitDate) then
      begin
        iCds.FieldByName(TPmItemDataModel.cRecordDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TPmItemDataModel.cRecordDateKey).AsDateTime:=fRecordDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cModifyPsnKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cModifyPsnKey).AsString:=fModifyPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cModifyDateKey)<>nil) then
    begin
      if SameDateTime(fModifyDate,fInitDate) then
      begin
        iCds.FieldByName(TPmItemDataModel.cModifyDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TPmItemDataModel.cModifyDateKey).AsDateTime:=fModifyDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TPmItemDataModel.cTimeStampKey)<>nil) then
    begin
      iCds.FieldByName(TPmItemDataModel.cTimeStampKey).AsLargeInt:=fTimeStamp;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TPmItemDataModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemData.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemData.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TPmItemDataModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TPmItemDataModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TPmItemDataModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TPmItemDataModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TPmItemDataModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TPmItemDataModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TPmItemDataModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TPmItemDataModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TPmItemDataModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('ItemNo')<>nil then
    begin
      fItemNo:=Json.ItemByName('ItemNo').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemName')<>nil then
    begin
      fItemName:=Json.ItemByName('ItemName').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemName']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemSpec')<>nil then
    begin
      fItemSpec:=Json.ItemByName('ItemSpec').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemSpec']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemStatus')<>nil then
    begin
      fItemStatus:=Json.ItemByName('ItemStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemSortNo')<>nil then
    begin
      fItemSortNo:=Json.ItemByName('ItemSortNo').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemSortNo']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemType')<>nil then
    begin
      fItemType:=Json.ItemByName('ItemType').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemType']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemSource')<>nil then
    begin
      fItemSource:=Json.ItemByName('ItemSource').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemSource']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemRemark')<>nil then
    begin
      fItemRemark:=Json.ItemByName('ItemRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ItemClassId')<>nil then
    begin
      fItemClassId:=Json.ItemByName('ItemClassId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ItemClassId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('MsunitId')<>nil then
    begin
      fMsunitId:=Json.ItemByName('MsunitId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'MsunitId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('PackageTypeId')<>nil then
    begin
      fPackageTypeId:=Json.ItemByName('PackageTypeId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'PackageTypeId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RanksId')<>nil then
    begin
      fRanksId:=Json.ItemByName('RanksId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RanksId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordPsn')<>nil then
    begin
      fRecordPsn:=Json.ItemByName('RecordPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordDate')<>nil then
    begin
      fRecordDate:=Json.ItemByName('RecordDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyPsn')<>nil then
    begin
      fModifyPsn:=Json.ItemByName('ModifyPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyDate')<>nil then
    begin
      fModifyDate:=Json.ItemByName('ModifyDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('TimeStamp')<>nil then
    begin
      fTimeStamp:=Json.ItemByName('TimeStamp').AsInt64;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'TimeStamp']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TPmItemData.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TPmItemDataModel.cModuleKey;
    Json.Add(TPmItemDataModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TPmItemDataModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TPmItemDataModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('ItemNo').AsString:=fItemNo;
    Json.Add('ItemName').AsString:=fItemName;
    Json.Add('ItemSpec').AsString:=fItemSpec;
    Json.Add('ItemStatus').AsInteger:=fItemStatus;
    Json.Add('ItemSortNo').AsInteger:=fItemSortNo;
    Json.Add('ItemType').AsInteger:=fItemType;
    Json.Add('ItemSource').AsInteger:=fItemSource;
    Json.Add('ItemRemark').AsString:=fItemRemark;
    Json.Add('ItemClassId').AsString:=fItemClassId;
    Json.Add('MsunitId').AsString:=fMsunitId;
    Json.Add('PackageTypeId').AsString:=fPackageTypeId;
    Json.Add('RanksId').AsString:=fRanksId;
    Json.Add('RecordPsn').AsString:=fRecordPsn;
    Json.Add('RecordDate').AsDateTime:=fRecordDate;
    Json.Add('ModifyPsn').AsString:=fModifyPsn;
    Json.Add('ModifyDate').AsDateTime:=fModifyDate;
    Json.Add('TimeStamp').AsInt64:=fTimeStamp;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TPmItemDataList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TPmItemDataList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TPmItemDataList.ResetObject;
begin
  Clear;
end;

function TPmItemDataList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TPmItemData;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TPmItemData.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemDataList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TPmItemDataList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TPmItemData;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TPmItemDataModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TPmItemDataModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TPmItemDataModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TPmItemDataModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TPmItemDataModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TPmItemDataModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TPmItemDataModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TPmItemDataModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TPmItemData.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TPmItemDataList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TPmItemDataModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TPmItemDataModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TPmItemDataModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TPmItemDataList.FindByKey(const iKey:string):TPmItemData;
var
  Item:TPmItemData;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TPmItemDataList.IsExist(iKey:string):Boolean;
var
  Item:TPmItemData;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TPmItemDataList.Add(const iItem:TPmItemData):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TPmItemDataList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TPmItemDataList.Remove(const iKey:string):TPmItemData;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TPmItemDataList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
